﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using Newtonsoft.Json;
using ERP_SaaS_WinForm.生产计划_派单_车间.Teams;
using ERP_SaaS_WinForm.生产计划_派单_车间.apiResquest;

namespace ERP_SaaS_WinForm.生产计划_派单_车间
{
    public partial class TeamManage : Form
    {
        private DataGridView dgvTeams;
        private Button btnRefresh;

        public TeamManage()
        {
            InitializeComponent();
            InitializeCustomComponents();
            LoadTeams();
        }

        private void InitializeCustomComponents()
        {
            // 设置窗体标题
            this.Text = "班组管理";
            this.Size = new Size(1000, 600);
            this.StartPosition = FormStartPosition.CenterScreen;

            // 创建DataGridView
            dgvTeams = new DataGridView();
            dgvTeams.Dock = DockStyle.Fill;
            dgvTeams.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvTeams.AllowUserToAddRows = false;
            dgvTeams.ReadOnly = true;
            dgvTeams.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvTeams.MultiSelect = false;
            dgvTeams.RowHeadersVisible = false;
            dgvTeams.BackgroundColor = Color.White;
            dgvTeams.GridColor = Color.LightGray;
            dgvTeams.BorderStyle = BorderStyle.None;

            // 创建刷新按钮
            btnRefresh = new Button();
            btnRefresh.Text = "刷新数据";
            btnRefresh.Size = new Size(100, 30);
            btnRefresh.Location = new Point(10, 10);
            btnRefresh.Click += BtnRefresh_Click;

            // 创建面板来容纳按钮
            Panel buttonPanel = new Panel();
            buttonPanel.Dock = DockStyle.Top;
            buttonPanel.Height = 50;
            buttonPanel.Controls.Add(btnRefresh);

            // 创建面板来容纳DataGridView
            Panel gridPanel = new Panel();
            gridPanel.Dock = DockStyle.Fill;
            gridPanel.Controls.Add(dgvTeams);

            // 添加控件到窗体
            this.Controls.Add(gridPanel);
            this.Controls.Add(buttonPanel);
        }

        private async void LoadTeams()
        {
            try
            {
                btnRefresh.Enabled = false;
                btnRefresh.Text = "加载中...";

                var response = await ApiRequestHelper.GetAsync<ApiTeamResponse>("api/Dispatch/GetTeam");

                if (response.Success && response.Data?.code == 200 && response.Data.data != null)
                {
                    SetupDataGridView();
                    LoadDataToGrid(response.Data.data);
                }
                else
                {
                    var errorMsg = response.Data?.msg ?? response.Message;
                    ApiRequestHelper.ShowError($"获取数据失败: {errorMsg}");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载数据时发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                btnRefresh.Enabled = true;
                btnRefresh.Text = "刷新数据";
            }
        }

        private void SetupDataGridView()
        {
            dgvTeams.Columns.Clear();

            // 添加列并设置标题
            dgvTeams.Columns.Add("TeamCode", "班组编号");
            dgvTeams.Columns.Add("TeamName", "班组名称");
            dgvTeams.Columns.Add("TeamType", "班组类型");
            dgvTeams.Columns.Add("Leader", "负责人");
            dgvTeams.Columns.Add("TeamNum", "班组人数");
            dgvTeams.Columns.Add("Name", "成员名称");

            // 设置列宽
            dgvTeams.Columns["TeamCode"].Width = 120;
            dgvTeams.Columns["TeamName"].Width = 150;
            dgvTeams.Columns["TeamType"].Width = 100;
            dgvTeams.Columns["Leader"].Width = 100;
            dgvTeams.Columns["TeamNum"].Width = 80;
            dgvTeams.Columns["Name"].Width = 200;

            // 设置标题行样式
            dgvTeams.ColumnHeadersDefaultCellStyle.BackColor = Color.LightBlue;
            dgvTeams.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black;
            dgvTeams.ColumnHeadersDefaultCellStyle.Font = new Font(dgvTeams.Font, FontStyle.Bold);
            dgvTeams.ColumnHeadersHeight = 35;
            dgvTeams.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

            // 设置交替行颜色
            dgvTeams.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
        }

        private void LoadDataToGrid(List<TeamData> teams)
        {
            dgvTeams.Rows.Clear();

            foreach (var team in teams)
            {
                int rowIndex = dgvTeams.Rows.Add();
                dgvTeams.Rows[rowIndex].Cells["TeamCode"].Value = team.TeamCode;
                dgvTeams.Rows[rowIndex].Cells["TeamName"].Value = team.TeamName;
                dgvTeams.Rows[rowIndex].Cells["TeamType"].Value = team.TeamType;
                dgvTeams.Rows[rowIndex].Cells["Leader"].Value = team.Leader;
                dgvTeams.Rows[rowIndex].Cells["TeamNum"].Value = team.TeamNum;
                dgvTeams.Rows[rowIndex].Cells["Name"].Value = team.Name;
            }
        }

        private void BtnRefresh_Click(object sender, EventArgs e)
        {
            LoadTeams();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
        }
    }
}
